﻿/* ************************************************************************************* 
 * CREATED BY:          LY HUNG SON
 * CREATED DATE:        14/07/2008
 * PROJECT:             MCCORP PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using Telerik.Web.UI.Grid;
using MCWEBSITE.BUL;
using MCWEBSITE.ENTITY;

public partial class Modules_WebsiteBackEnd_wucNewsCategoriesList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.LoadFilterSection();
            this.BindGridviewList();
        }
    }

    // ============================================================================================================
    #region "Event Declaration : RadGridItemsSelected"

    public delegate void RadGridItemSelectedEventHandler(object sender, RadGridItemsSelectedEventArgs e);
    public event RadGridItemSelectedEventHandler RadGridItemsSelected;

    protected virtual void OnRadGridItemsSelected(RadGridItemsSelectedEventArgs e)
    {
        if (RadGridItemsSelected != null) RadGridItemsSelected(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Event Declaration : RadGridItemsDeleted"

    public delegate void RadGridItemDeletedEventHandler(object sender, RadGridItemsDeletedEventArgs e);
    public event RadGridItemDeletedEventHandler RadGridItemsDeleted;

    protected virtual void OnRadGridItemsDeleted(RadGridItemsDeletedEventArgs e)
    {
        if (RadGridItemsDeleted != null) RadGridItemsDeleted(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Event Declaration : RadGridItemEdited"

    public delegate void RadGridItemEditedEventHandler(object sender, RadGridItemEditedEventArgs e);
    public event RadGridItemEditedEventHandler RadGridItemEdited;

    protected virtual void OnRadGridItemEdited(RadGridItemEditedEventArgs e)
    {
        if (RadGridItemEdited != null) RadGridItemEdited(this, e);
    }

    #endregion

    // ============================================================================================================
    #region "Control Events"

    protected void rcboLanguageCode_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        this.BindParentCategory(rcboLanguageCode.SelectedValue);
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    protected void rcboParentCategory_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    protected void rgrdGridDataList_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        rgrdGridDataList.CurrentPageIndex = e.NewPageIndex;
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    protected void rgrdGridDataList_ItemCommand(object source, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "RowClick":
                RadGridItemsSelectedEventArgs objSelectedEventArg = null;
                int SelectedItemsCount = rgrdGridDataList.MasterTableView.GetSelectedItems().Length;
                objSelectedEventArg = new RadGridItemsSelectedEventArgs(SelectedItemsCount);
                // Fire event
                OnRadGridItemsSelected(objSelectedEventArg);
                break;
            case "RowEdit":
                // Set edit item back to normal
                RadGridItemEditedEventArgs objEditedEventArg = null;
                int ItemID = Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]);
                objEditedEventArg = new RadGridItemEditedEventArgs(Convert.ToString(ItemID));
                rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].Edit = false;
                // Bind grid again
                this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
                // Fire event
                OnRadGridItemEdited(objEditedEventArg);
                break;
            case "RowDelete":
                // Delete row
                RadGridItemsDeletedEventArgs objDeletedEventArg = null;
                int DeletedItemsCount = DeleteSelectedItems(Convert.ToInt32(rgrdGridDataList.MasterTableView.Items[e.Item.ItemIndex].KeyValues.Split('\"')[1]));
                objDeletedEventArg = new RadGridItemsDeletedEventArgs(DeletedItemsCount, 0);
                // Bind grid again
                this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
                // Fire event
                OnRadGridItemsDeleted(objDeletedEventArg);
                break;
        }
    }
    #endregion

    #region "Public function/procedure and utilities"

    /// <summary>
    /// 
    /// </summary>
    public void BindGridviewList()
    {
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="LanguageCode"></param>
    /// <param name="ParentCategoryID"></param>
    public void BindGridviewList(string LanguageCode, string ParentCategoryID)
    {
        rgrdGridDataList.DataSource = websiteNewsCategoryBUL.GetCategoryByLangAndParent(LanguageCode, ParentCategoryID);
        rgrdGridDataList.DataBind();
    }

    /// <summary>
    /// Delete record by list of ID
    /// </summary>
    /// <returns></returns>
    public void DeleteSelectedItems(ArrayList SelectedItemList)
    {
        int TotalSelectedItems = SelectedItemList.Count;
        int DeletedItems = 0;
        for (int i = 0; i < SelectedItemList.Count; i++)
        {
            DeletedItems = DeletedItems + DeleteSelectedItems(Convert.ToInt32(SelectedItemList[i]));
        }
        RadGridItemsDeletedEventArgs objDeletedEventArg = null;
        objDeletedEventArg = new RadGridItemsDeletedEventArgs(DeletedItems, TotalSelectedItems - DeletedItems);
        // Bind grid again
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
        // Fire event
        OnRadGridItemsDeleted(objDeletedEventArg);
    }

    /// <summary>
    /// Delete record by ID
    /// </summary>
    /// <param name="ModuleInstanceID"></param>
    /// <returns></returns>
    public int DeleteSelectedItems(int SelectedItemID)
    {
        return websiteNewsCategoryBUL.Remove(SelectedItemID);
    }

    /// <summary>
    /// excute filter gridview
    /// </summary>
    public void FilterGridView()
    {
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    /// <summary>
    /// Refresh gridview
    /// </summary>
    public void RefreshGridView()
    {
        this.BindGridviewList(rcboLanguageCode.SelectedValue, rcboParentCategory.SelectedValue);
    }

    /// <summary>
    /// Get selected item in grid
    /// </summary>
    /// <returns></returns>
    public ArrayList GridSelectedItems()
    {
        int countCheckedItem = rgrdGridDataList.MasterTableView.GetSelectedItems().Length;
        ArrayList SelectedArray = new ArrayList();

        foreach (GridDataItem _GridDataItem in rgrdGridDataList.MasterTableView.GetSelectedItems())
        {
            SelectedArray.Add(_GridDataItem.KeyValues.Split('\"')[1]);
        }
        return SelectedArray;
    }

    public string GetSelectedLanguageCode()
    {
        return rcboLanguageCode.SelectedValue;
    }

    public string GetSelectedParentCategory()
    {
        return rcboParentCategory.SelectedValue;
    }

    protected void LoadFilterSection()
    {
        this.BindLanguageCode();
        this.BindParentCategory(rcboLanguageCode.SelectedValue);
    }

    protected void BindLanguageCode()
    {
        // Load rcboLanguageCode
        rcboLanguageCode.Items.Clear();
        List<websiteLanguageInfo> LanguageList = websiteLanguageBUL.GetAll();
        websiteLanguageInfo SelectAllLanguage = new websiteLanguageInfo();
        SelectAllLanguage.LanguageCode = "TOÀN BỘ";
        SelectAllLanguage.languageName = "TOÀN BỘ";
        SelectAllLanguage.Description = "";
        LanguageList.Add(SelectAllLanguage);
        rcboLanguageCode.DataSource = LanguageList;
        rcboLanguageCode.DataBind();
        rcboLanguageCode.SelectedIndex = rcboLanguageCode.FindItemIndexByValue("TOÀN BỘ");
    }

    protected void BindParentCategory(string LanguageCode)
    {
        // Load rcboParentCategory
        rcboParentCategory.Items.Clear();
        List<websiteNewsCategoryInfo> ParentCategoryList = websiteNewsCategoryBUL.GetParentByLanguageCode(LanguageCode);
        websiteNewsCategoryInfo SelectAllNewsCategory = new websiteNewsCategoryInfo();
        SelectAllNewsCategory.CategoryID = 0;
        SelectAllNewsCategory.CategoryName = "TOÀN BỘ";
        SelectAllNewsCategory.CategoryCode = "TOÀN BỘ";
        SelectAllNewsCategory.Description = "";
        SelectAllNewsCategory.LanguageCode = LanguageCode;
        SelectAllNewsCategory.ParentCategoryID = 0;
        SelectAllNewsCategory.Atribute1 = "";
        SelectAllNewsCategory.Atribute2 = "";
        SelectAllNewsCategory.Atribute3 = "";
        ParentCategoryList.Add(SelectAllNewsCategory);
        rcboParentCategory.ClearSelection();
        rcboParentCategory.DataSource = ParentCategoryList;
        rcboParentCategory.DataBind();
        rcboParentCategory.SelectedIndex = rcboParentCategory.FindItemIndexByText("TOÀN BỘ");
    }

    #endregion

}
